package com.example.demo.service.impl;

import com.example.demo.api.RemoteOrderService;
import com.example.demo.api.RemoteStorageService;
import com.example.demo.api.model.SeataOrder;
import com.example.demo.service.BusinessService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zxk
 */
@Service
public class BusinessServiceImpl implements BusinessService {

    @Autowired
    private RemoteOrderService orderService;
    @Autowired
    private RemoteStorageService storageService;

    @GlobalTransactional(rollbackFor = Exception.class)
    @Override
    public SeataOrder purchase(Integer userId, String commodityCode, int orderCount) {
        storageService.deduct(commodityCode, orderCount);
        return orderService.create(userId, commodityCode, orderCount);
    }
}
